﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Jzy.Core.Common.Web;
using Jzy.Core.Common;
using Jzy.Workflow.Flow;

using System.Xml.Linq;
using System.Text;
using Jzy.Workflow.Task;
using Frame.Lib.Enums;
using Frame.Lib.Web;
using Frame.Lib.Utilities;
using Frame.Lib.Common;

namespace Jzy.Web.WorkFlow.Fore
{
    public partial class FormDetail : System.Web.UI.Page
    {
        private string TaskID//有可能要加密
        {
            get
            {
                return Request["TaskID"];
            }
        }

        private FlowTaskBO _task;
        private FlowTaskBO Task
        {
            get
            {
                if (_task == null)
                {
                    _task = new FlowTaskBO();
                }
                return _task;
            }
        }
        private FlowTaskInfo _taskInfo;
        private FlowTaskInfo TaskInfo
        {
            get
            {
                if (_taskInfo == null)
                {
                    if (!string.IsNullOrEmpty(TaskID))
                    {
                        _taskInfo = Task.GetInfo(TaskID);
                    }
                }
                return _taskInfo;
            }
            set
            {
                _taskInfo = value;
            }
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            CssFileManager.AddCssFile(CssFile.tab1);
            if (!IsPostBack)
            {
                InitData();
            }

            JSUtil.SetDivHeight("divMain", 88);
        }

        private void InitData()
        {
            this.Attachs1.InitData();

            List<EnumInfo> list = ConvertUtil.ConvertEnum(typeof(FormTab));
            
            list.Where(e => e.ID == (int)FormTab.相关附件).First().Name += "<span style='color:Red;font-weight:bold;'>&nbsp;(" + Attachs1.AttachCount + ")</span>";
            
            rpt.DataSource = list;
            rpt.DataBind();

            XElement html = XElement.Parse(this.TaskInfo.FormInfo.SnapInfo.FormTemplate);
            
            litHead.Text = html.Element(ElementItem.Head).Value;
            litForm.Text = html.Element(ElementItem.Body).Value;          

            this.InitFormData();

        }

        /// <summary>
        ///  初始化表单数据
        /// </summary>
        /// <param name="info"></param>
        private void InitFormData()
        {
            string script = null;//向客户端输入的JSON脚本
            if (this.TaskInfo != null)//如是不是新建，或从草稿箱中过来，则还原数据
            {
                StringBuilder sbFormData = new StringBuilder();
                sbFormData.Append("formData = [");
                XElement formData = XElement.Parse(this.TaskInfo.FormInfo.FormData);//表单数据
                foreach (XElement fld in formData.Elements())//对数据内容的过滤暂未处理
                {
                    sbFormData.Append("{name:'" + fld.Name.LocalName + "',type:'" + fld.Attribute("type").Value + "',value:'" + fld.Value.Replace("'", "\\'").Replace("\"", "\\\"") + "'},");
                }
                script = sbFormData.ToString().TrimEnd(',') + "];initData();";
                JSUtil.ExcuteJS(script, "formData");
            }
        }
    }
}
